﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace mydddd.DAL
{
	/// <summary>
	/// 数据访问类:sysModule
	/// </summary>
	public partial class SysModule
	{

        /// <summary>
        /// 更新绝对路径
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
	    public bool UpdateModulePath(int id)
	    {
	        string sql =string.Format(@"UPDATE sysModule
                                                SET	
	                                                modulePath = modulePath+'{0},'
                                                WHERE id={0}",id);
            return DbHelperSQL.ExecuteSql(sql)>0;
	    }

        /// <summary>
        /// 更新父节点为不是跟节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
	    public bool UpdateParentNoEnd(int id)
	    {
	        string sql = string.Format(@"UPDATE sysModule
                                    SET	
                                         isend=0
                                WHERE id=(SELECT parentID FROM sysModule AS sm WHERE sm.id={0} )", id);
            return DbHelperSQL.ExecuteSql(sql) > 0;
	    }

	    /// <summary>
        /// 得到一个对象实体(父级的绝对路径)
        /// </summary>
        public mydddd.Model.SysModule GetModulePathModel(int id)
        {
            //获取父级的绝对路径
            string sql = @"SELECT
	                        sm.id,
	                        sm.parentID,
	                        sm.moduleName,
	                        (SELECT s1.modulePath FROM sysModule s1 WHERE s1.id=sm.parentID) AS modulePath,
	                        --sm.modulePath,
	                        sm.moduleUrl,
	                        sm.moduleIcon,
	                        sm.sort,
	                        sm.[status],
	                        sm.info,
	                        sm.isEnd
                        FROM
	                        sysModule AS sm where sm.id=@id";
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            mydddd.Model.SysModule model = new mydddd.Model.SysModule();
            DataSet ds = DbHelperSQL.Query(sql, parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 页面角色id 获取页面组件数据
        /// </summary>
        /// <param name="sysRolepopedom_id">页面权限id</param>
        /// <returns></returns>
        public DataSet GetListBySysRolepopedomId(int sysRolepopedom_id)
        {
            string sql = @"SELECT    sm.*
                                            FROM   SysModule AS sm
                                            WHERE  (
                                                       SELECT ',' + moduleid + ','
                                                       FROM   SysRolepopedom AS sr
                                                       WHERE  sr.id = @id
                                                   ) LIKE '%,' + CAST(sm.id AS VARCHAR(20)) + ',%' ";

            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = sysRolepopedom_id;

            return DbHelperSQL.Query(sql, parameters);
        }

        /// <summary>
        /// 判断页面权限
        /// </summary>
        /// <param name="rid">权限角色id</param>
        /// <param name="url">当前页面url</param>
        /// <returns></returns>
        public  bool IsRole(int rid, string url)
        {
            string sql = @"SELECT COUNT(1)
                            FROM   SysModule AS sm
                            WHERE  (
                                       SELECT ',' + moduleid + ','
                                       FROM   SysRolepopedom AS sr
                                       WHERE  sr.id = @id
                                   ) LIKE '%,' + CAST(sm.id AS VARCHAR(20)) + ',%' and moduleUrl=@moduleUrl  ";

            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@moduleUrl", SqlDbType.NVarChar,200)
			};
            parameters[0].Value = rid;
            parameters[1].Value = url;
            object obj = DbHelperSQL.GetSingle(sql, parameters);
            if (obj == null)
            {
                return false;
            }
            else
            {
                return Convert.ToInt32(obj) > 0;
            }
        }
	}
}

